package com;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LoginPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	public static final int HAVEOPPONENT=1;
	public static final int AGREE=2;
	public static final int REFUSE=3;
	private MainFrame mf;
	private String name,ip;
	private User user;
	JDialog dialog;
	JTextField[] tf=new JTextField[2];
	private JButton[] bt=new JButton[2];
	Integer agreeState=0;//1同意 2不同意
	public LoginPanel() {
		String str[]={"昵  称:","对方IP:"};
		String str1[]={"登录","关闭"};
		GridBagConstraints gbc;
		setLayout(new GridBagLayout());
		setOpaque(false);
		setForeground(Color.gray);
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		Font font=new Font("宋体", Font.BOLD, 24);
		for(int i=0;i<str.length;i++){
			gbc=new GridBagConstraints();
			gbc.gridx=0; gbc.gridy=i;
			gbc.anchor=GridBagConstraints.EAST;
			JLabel label=new JLabel(str[i]);
			label.setFont(font);
			label.setForeground(Color.white);
			add(label,gbc);
		}
		font=new Font("宋体", Font.PLAIN, 18);
		for(int i=0;i<str.length;i++){
			gbc=new GridBagConstraints();
			gbc.fill=GridBagConstraints.HORIZONTAL;
			gbc.gridx=1;gbc.gridy=i;
			gbc.gridwidth=2;
			gbc.insets=new Insets(10, 0, 10, 0);
			tf[i]=new JTextField();
			tf[i].setFont(font);
			add(tf[i],gbc);
		}
		for(int i=0;i<str.length;i++){
			gbc=new GridBagConstraints();
			gbc.gridx=1+i;gbc.gridy=2;
			gbc.insets=new Insets(0, 0, 0, 50);
			bt[i]=new JButton(str1[i]);
			bt[i].addActionListener(this);
			add(bt[i],gbc);
		}
	}
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2=(Graphics2D) g;
		Composite composite=g2.getComposite();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f));
		g2.fillRect(0, 0, getWidth(), getHeight());
		g2.setComposite(composite);
		super.paintComponent(g);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String ac=e.getActionCommand();
		if(ac.equals("登录")){
			mf=(MainFrame) getParent().getParent();
			name=tf[0].getText();
			if(name.trim().isEmpty()){
				JOptionPane.showMessageDialog(null, "请输入昵称！");
				return;
			}
			ip=tf[1].getText();
			if(ip.trim().isEmpty()){
				JOptionPane.showMessageDialog(null, "请输入对方ip！");
				return;
			}
			try {
				user=new User();
				Time time=new Time(System.currentTimeMillis());
				user.setName(name);
				user.setHost(InetAddress.getLocalHost());
				user.setTime(time);
				user.setOpponent(false);
				if(mf.isLinked){
					mf.setUser(user);
					mf.send(mf.getSocket(), user);
					setVisible(false);
				}
				else{
					request();
				}
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			}
			
		}
		else if(ac.equals("关闭")){
			System.exit(0);
		}
	}
	private void request(){
		try {
			InetAddress ia=InetAddress.getByName(ip);
			Socket socket=new Socket(ia, mf.getPort());
			mf.setSocket(socket);
			mf.send(socket,user);
			dialog=new JDialog(mf,true);
			dialog.setSize(400, 100);
			dialog.setUndecorated(true);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
			dialog.setLocation((d.width-400)/2, (d.height-50)/2);
			JLabel la=new JLabel("正在等待对方同意，请稍后...");
			la.setFont(new Font("隶书", Font.BOLD, 24));
			la.setHorizontalAlignment(JLabel.CENTER);
			dialog.add(la);
			dialog.setVisible(true);
			if(agreeState==REFUSE) {
				socket.close();
				mf.setSocket(null);
				JOptionPane.showMessageDialog(null, "对方拒绝了您的请求！");
				return;
				}
			else if(agreeState==AGREE)
			{ 
			   mf.setUser(user);
			   mf.isLinked=true;
			   setVisible(false);
			}
		} catch (UnknownHostException e1) {
			System.out.println("不能识别的主机！");
		} catch (IOException e1) {
			System.out.println("连接失败！");
		}
	}
}
